package common;


import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import io.restassured.response.Response;
import org.apache.commons.configuration.ConfigurationException;
import org.testng.annotations.*;
import utils.GetFileMess;
import io.restassured.RestAssured;
import utils.WritePropertiesUtil;

import static utils.WritePropertiesUtil.writePropertiesFile;

/*
 *父类的注解可以被子类继承，所有的环境变量在父类进行配置
 *根据testng.xml文件传入的参数来选择环境参数，默认为yg_env
 */
public class SetUpTearDown {

    public int sheetIndex;
//    public String timeSlice;
//    public String medCode;
//    public String category;
//
//    @Parameters({"timeSlice","medCode","category"})
//    public void a( String timeSlice,String medCode,String category) {
//        this.timeSlice = timeSlice;
//        this.medCode = medCode;
//        this.category = category;
//    }
//    @BeforeSuite
//    public void dataSetUp() throws SQLException, IOException, ClassNotFoundException {
//        ExcutSqlFile.excute("setUpSQL"); //案例执行前参数维护
//    }

    //环境配置
    @BeforeClass
    @Parameters({"sheetIndex"})
    public void beforeClass(int sheetIndex) {
        this.sheetIndex = sheetIndex;
    }
    public  Data envSetUp()  {
        JSONObject json= new JSONObject();
        String system = "env.properties";
        try {
            RestAssured.baseURI = new GetFileMess().getValue("baseURI", system);
            RestAssured.basePath = new GetFileMess().getValue("basePath", system);
            RestAssured.port = Integer.parseInt(new GetFileMess().getValue("port", system));
//            System.out.println(RestAssured.basePath);
            String userName=new GetFileMess().getValue("userName", system);
            String passwd=new GetFileMess().getValue("passwd", system);
            String orgId=new GetFileMess().getValue("orgId", system);
            json.put("userName", userName);
            json.put("passwd", passwd);
            json.put("orgId", orgId);
        } catch ( ConfigurationException e) {
            e.printStackTrace();
        }
        Response response= RunCaseJson.runCase(json,"post","api-auth/login");
        String jsonStr= response.asString();
        System.out.println("响应报文:"+jsonStr);
//        JSONObject jsonObj=JSONObject.parseObject(jsonStr);
        Loginobject loginobject= JSONObject.parseObject(jsonStr,new TypeReference<Loginobject>(){}.getType());
        Data data= loginobject.getData();
        return data;
    }
//    public Data beforeclass() {
//        JSONObject json= new JSONObject();
//        json.put("userName", "112");
//        json.put("passwd", "tP123456@");
//        json.put("orgId", "75635");
//
//        Response response= RunCaseJson.runCase(json,"post","unity-auth/login");
//        String jsonStr= response.asString();
////        JSONObject jsonObj=JSONObject.parseObject(jsonStr);
//        Loginobject loginobject= JSONObject.parseObject(jsonStr,new TypeReference<Loginobject>(){}.getType());
//        Data data= loginobject.getData();
//        return data;
//    }


//    public void beforeClass(int sheetIndex,String timeSlice,String medCode) {
//        this.sheetIndex = sheetIndex;
//        this.timeSlice=timeSlice;
//        this.medCode=medCode;
//    }

    /*
     *创建environment.properties并放到allure-results目录下，测试报告展现
     */
    @AfterSuite
    public void createEnvPropertiesForReport() throws IOException, ConfigurationException {
        Map<String, String> data = new HashMap<>();
        String env="env.properties";
        data.put("Environment", new GetFileMess().getValue("Environment", env));
        data.put("baseURI", RestAssured.baseURI + ":" + RestAssured.port +  RestAssured.basePath);
        data.put("basePath",new GetFileMess().getValue("basePath", env));
        data.put("port",new GetFileMess().getValue("port", env));
        data.put("userName",new GetFileMess().getValue("userName", env));
        data.put("passwd",new GetFileMess().getValue("passwd", env));
        data.put("orgId",new GetFileMess().getValue("orgId", env));
        writePropertiesFile(data);

        WritePropertiesUtil copyReportResources=new WritePropertiesUtil();
        copyReportResources.copyResources();

    }

//    @AfterSuite
//    public void dataTearDown() throws SQLException, IOException, ClassNotFoundException {
//        //案例执行结束后，对数据池的数据进行清理（删除或更新状态）
//        ExcutSqlFile.excute("tearDownSQL"); //案例执行后数据清理
//    }

    public static void main (String[] args){
//        envSetUp();
    }
}